﻿//need jquery
var tempId = "0";
var cookiekeyrefid = "byhandrefid";
$.fn.exists = function () {
    return this.length !== 0;
}
function get_data(source, dataname) {
    return $(source).attr('data-' + dataname);
}
function call_payment(that, productid, tempID) {

    if (tempID != null && tempID != "" && tempID != "0") {
        tempId = tempID;
    }
    //check if use button embled html/js
    var refid = $(that).attr('data-refid');
    //if found just use , if not found frame button or click on byhand
    if (refid == null) {
        //check cookie
        refid = getCookie(cookiekeyrefid);
        //if not found set refid=0 , else found just use
        if (refid == null || refid == "" || refid == "0") {
            refid = 0;
        }
    }
    popup_payment(productid, refid);
}
function popup_payment(id, refid) {
    var size = {
        width: window.innerWidth || document.body.clientWidth,
        height: window.innerHeight || document.body.clientHeight
    }
    var prelocation = getCookie("previouslocation");
    if (prelocation == null) {
        prelocation = top.window.location;
    }
    if (size.width <= 600) {
        window.location = '/Payment/WidgetPopup?id=' + id + '&refid=' + refid + '&tempID=' + tempId + '&opentype=3&location=' + prelocation;
        return;
    }

    var container_payment;
    container_payment = $('.byhand-container-frame[data-id=' + id + ']');
    if (container_payment.length === 0) {

        $('body').append('<div class="modal fade byhand-container-frame" data-id="' + id + '" > <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title">Mua hàng tại ByHand.vn</h4> </div><div class="byhand-wrapper-widgetpopup"></div></div> </div> </div>');
        //$('body').append('<div class="byhand-container-frame" data-id="' + id + '" style="height: auto; width: auto;"></div>');
        container_payment = $('.byhand-container-frame[data-id=' + id + ']');
        $(container_payment).find('.byhand-wrapper-widgetpopup').html('<iframe src="/Payment/WidgetPopup?id=' + id + '&refid=' + refid + '&tempID=' + tempId + '&opentype=3&location=' + prelocation + '" width="578" height="570" frameborder="0" scrolling="auto" id="the_iframe" height="auto"></iframe>');
        //$(container_payment).dialog({ width: 750, height: 800, closeOnEscape: true, modal: true, resizable: false });
        $(container_payment).modal({ keyboard: true });
    } else {
        var content = $(container_payment);
        //$(content).dialog();
        $(content).modal({ keyboard: true });
    }
}
function postlog(orderid, actiontype, username, usertype) {
    $.post("/Log/LogOrder", { orderid: orderid, actiontype: actiontype, username: username, usertype: usertype }, function (data) {

    });
}
$(document).ready(function () {
    if (tempId == "0") {
        tempId = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }
});
//cookie
function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = c_name + "=" + c_value;
}
function getCookie(c_name) {
    var c_value = document.cookie;
    var c_start = c_value.indexOf(" " + c_name + "=");
    if (c_start == -1) {
        c_start = c_value.indexOf(c_name + "=");
    }
    if (c_start == -1) {
        c_value = null;
    }
    else {
        c_start = c_value.indexOf("=", c_start) + 1;
        var c_end = c_value.indexOf(";", c_start);
        if (c_end == -1) {
            c_end = c_value.length;
        }
        c_value = unescape(c_value.substring(c_start, c_end));
    }
    return c_value;
}